//
//  DataParser.m
//  SendGzipData
//
//  Created by URANUS on 15/4/13.
//  Copyright (c) 2015年 URANUS. All rights reserved.
//

#import "DataParser.h"

@implementation DataParser

- (instancetype) initDataParser:(NSData*) data{
    self = [super init];
    self.data = data;
    self.parser = [[NSXMLParser alloc] initWithData:data];
    self.parser.delegate = self;
    return self;
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict {
    self.currentValue = [[NSMutableString alloc] initWithString:@""];
    self.currentKey = elementName;
    
    if([elementName isEqualToString:@"TransData"]){
        self.transInfo = [[NSDictionary alloc] init];
    }else if([elementName isEqualToString:@"BaseInfo"]){
        self.transData = [[TransData alloc] init];
    }
}

- (void) parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    DDLogInfo(@"self.currentValue is %@ and string is %@", self.currentValue, string);
    [self.currentValue appendString:string];
}

- (void) parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
    DDLogInfo(@"elementName is %@ and Current value is %@", elementName, self.currentValue);
    if ([elementName isEqualToString:@"TransType"]) {
        self.transData.transType = self.currentValue;
    } else if ([elementName isEqualToString:@"TransCode"]){
        self.transData.transCode = self.currentValue;
    } else if ([elementName isEqualToString:@"SubTransCode"]){
        self.transData.subTransCode = self.currentValue;
    } else if ([elementName isEqualToString:@"UserName"]){
        self.transData.userName = self.currentValue;
    } else if ([elementName isEqualToString:@"Password"]){
        self.transData.password = self.currentValue;
    } else if ([elementName isEqualToString:@"ResultCode"]){
        self.transData.resultCode = self.currentValue;
    } else if ([elementName isEqualToString:@"ResultMsg"]){
        self.transData.resultMsg = self.currentValue;
    } else if ([elementName isEqualToString:@"OutputData"]){
        self.outputData = self.currentValue;
    }
}

- (void) parserDidStartDocument:(NSXMLParser *)parser {
    DDLogInfo(@"开始解析XML文件");
}

- (void) parserDidEndDocument:(NSXMLParser *)parser{
    DDLogInfo(@"解析XML文件完成");
}

@end
